package com.xjh.him.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.him.dao.UserGroupMemberMapper;
import com.xjh.him.dao.UserGroupMsgMapper;
import com.xjh.him.domain.UserGroupMemberDO;
import com.xjh.him.domain.UserGroupMsgDO;
import com.xjh.him.dto.usergroupmsg.UserGroupMsgDTO;
import com.xjh.him.dto.usergroupmsg.UserGroupMsgQueryDTO;
import com.xjh.him.netty.HandlerListener;
import com.xjh.him.netty.NettyCore;
import com.xjh.him.netty.codec.Command;
import com.xjh.him.netty.codec.Packet;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.ResultDTO;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserGroupMsgServiceImpl implements IUserGroupMsgService, HandlerListener {

    @Autowired
    private UserGroupMsgMapper userGroupMsgMapper;

    @Autowired
    private UserGroupMemberMapper userGroupMemberMapper;

    @Override
    public ResultDTO<List<UserGroupMsgDTO>> listMsg(UserGroupMsgQueryDTO queryDTO) {
        if (Checks.isAnyEmpty(queryDTO, queryDTO.getGroupId())) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        QueryWrapper<UserGroupMsgDO> listQW = new QueryWrapper<>();
        listQW.eq("user_group_id", queryDTO.getGroupId());
        listQW.orderByDesc("id");
        listQW.last(String.format("LIMIT %s, %s", queryDTO.getSkip(), queryDTO.getLimit()));
        List<UserGroupMsgDO> msgDOList = userGroupMsgMapper.selectList(listQW);
        List<UserGroupMsgDTO> dtoList = msgDOList.stream().map(item -> {
            UserGroupMsgDTO msgDTO = new UserGroupMsgDTO();
            BeanUtils.copyProperties(item, msgDTO);
            return msgDTO;
        }).collect(Collectors.toList());
        return ResultDTO.getSuccessResult(dtoList);
    }


    @Override
    public Boolean consume(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof UserGroupMsgDTO) {
            UserGroupMsgDTO msgDTO = (UserGroupMsgDTO) msg;
            //写入数据库
            UserGroupMsgDO msgDO = new UserGroupMsgDO();
            BeanUtils.copyProperties(msgDTO, msgDO);
            int insert = userGroupMsgMapper.insert(msgDO);
            if (insert == 0) {
                log.error("insert msg failed, msg:{}", msgDTO);
            }

            //通知其他群成员
            QueryWrapper<UserGroupMemberDO> listQW = new QueryWrapper<>();
            listQW.select("user_id");
            listQW.eq("user_group_id", msgDTO.getUserGroupId());
            List<UserGroupMemberDO> memberDOList = userGroupMemberMapper.selectList(listQW);
            for (UserGroupMemberDO memberDO : memberDOList) {
                if (!msgDTO.getUserId().equals(memberDO.getUserId())) {
                    Channel channel = NettyCore.ACTIVE_USER_CHANNEL.get(memberDO.getUserId());
                    if (channel != null) {
                        channel.writeAndFlush(Packet.create(Command.USER_GROUP_MSG_SEND, msgDTO));
                    }
                }
            }
            return true;
        }

        return false;
    }
}
